package com.jingdianjichi.subject.service.impl;
import com.alibaba.fastjson.JSON;
import com.jingdianjichi.subject.common.enums.isDeletedFlagEnum;
import com.jingdianjichi.subject.common.enums.CategoryTypeEnum;
import com.jingdianjichi.subject.converter.SubjectCategoryConverter;
import com.jingdianjichi.subject.eneity.SubjectCategoryBO;
import com.jingdianjichi.subject.infra.basic.entity.SubjectCategory;
import com.jingdianjichi.subject.infra.basic.service.SubjectCategoryService;
import com.jingdianjichi.subject.service.SubjectCategoryDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public  class SubjectCategoryDomainServiceImpl implements SubjectCategoryDomainService {
    @Resource
    private SubjectCategoryService SubjectCategoryService;

    @Override
    public void add(SubjectCategoryBO subjectCategoryBO) {
        if(log.isInfoEnabled()){
            log.info("SubjectCategoryController.add.bo:{} ", JSON.toJSONString(subjectCategoryBO));
        }
        SubjectCategory subjectCategory = SubjectCategoryConverter.INSTANCE.converterBoToCategory(subjectCategoryBO);
        //手动插入is_deleted
        subjectCategory.setIsDeleted(isDeletedFlagEnum.UN_DELETED.getCode());
        SubjectCategoryService.insert(subjectCategory);
    }
@Override
    public List<SubjectCategoryBO> queryCategory(SubjectCategoryBO subjectCategoryBO) {
        SubjectCategory subjectCategory = SubjectCategoryConverter.INSTANCE.converterBoToCategory(subjectCategoryBO);
        //设置查询条件为is_deleted为0表示查询没有删除的数据
        subjectCategory.setIsDeleted(isDeletedFlagEnum.UN_DELETED.getCode());
        List<SubjectCategory>  subjectCategoryList = SubjectCategoryService.queryCategory(subjectCategory);
        List<SubjectCategoryBO> subjectCategoryBOList = SubjectCategoryConverter.INSTANCE.converterSubjcetsToBos(subjectCategoryList);
        if(log.isInfoEnabled()){
            log.info("SubjectCategoryController.queryCategory.boList:{} ", JSON.toJSONString(subjectCategoryBOList));
        }
        return subjectCategoryBOList;
    }
    @Override
    public Boolean update(SubjectCategoryBO subjectCategoryBO) {
        SubjectCategory subjectCategory = SubjectCategoryConverter.INSTANCE.converterBoToCategory(subjectCategoryBO);
        Integer update = SubjectCategoryService.update(subjectCategory);
        return update>0;
    }

    /**
     * 逻辑删除 本质调用update更新is_deleted字段
     * @param subjectCategoryBO
     * @return
     */
    @Override
    public Boolean delete(SubjectCategoryBO subjectCategoryBO) {
        SubjectCategory subjectCategory = SubjectCategoryConverter.INSTANCE.converterBoToCategory(subjectCategoryBO);
        subjectCategory.setIsDeleted(isDeletedFlagEnum.DELETED.getCode());
        Integer update = SubjectCategoryService.update(subjectCategory);

        return update>0;
    }
}
